﻿using System;
using System.Linq;
using UniMana.Utils.Controls;
namespace UniMana.Data
{
    public class Training_SubjectOpenRequest_Teacher_StudentInfo
    {
        private bds m_bds = new bds();
        private DBMain m_data = new DBMain();
        private bool m_bExistInDatabase;
        public Training_SubjectOpenRequest_Teacher_StudentInfo(
            int _iTraining_SubjectOpenRequestKey = -1, 
            string _strTraining_StudentKey = Utils.Configs.DefaultConfig.EMPTY_GUID, 
            int _iTraining_TrainingProgramKey = -1)
        {
            var iQuery = 
                this.m_data.m_DCSave.Training_SubjectOpenRequest_Teacher_Students.Where(
                p => p.Training_SubjectOpenRequestKey == _iTraining_SubjectOpenRequestKey &&
                    p.Training_TrainingProgramKey == _iTraining_TrainingProgramKey &&
                    p.Training_StudentKey == Guid.Parse( _strTraining_StudentKey));
            this.m_bds.DataSource = iQuery;
            var Row = iQuery.FirstOrDefault();
            this.m_bExistInDatabase = true;
            if (Row == null)
            {
                Row = new Training_SubjectOpenRequest_Teacher_Student();
                Row.Training_StudentKey = Guid.Parse(_strTraining_StudentKey);
                Row.Training_SubjectOpenRequestKey = _iTraining_SubjectOpenRequestKey;
                Row.Training_TrainingProgramKey = _iTraining_TrainingProgramKey;
                this.m_bds.Add(Row);
                this.m_bds.MoveLast();
                this.m_bExistInDatabase = false;
            }                                                 
        }


        public bool ExistInDatabase
        {
            get { return this.m_bExistInDatabase; }
        }

        public void Save()
        {
            m_data.m_DCSave.SubmitChanges();
        }
        public void Delete()
        {
            m_bds.DeleteCurrent();
            m_data.m_DCSave.SubmitChanges();
        }
        public Training_SubjectOpenRequest_Teacher_Student Properties
        {
            get { return m_bds.Current as Training_SubjectOpenRequest_Teacher_Student; }
        }
    }
}
